package com.IntelligenceInsight.Myth.controller;

import com.IntelligenceInsight.Myth.model.Job;
import com.IntelligenceInsight.Myth.model.Model;
import com.IntelligenceInsight.Myth.service.JobService;
import com.IntelligenceInsight.Myth.service.ModelService;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
public class ModelController {
    @Autowired
    private ModelService service;
    @Autowired
    private JobService jobService;

    @RequestMapping(value = {"/modelAdd.do"}, method = RequestMethod.POST)
    public NoteRequest ModelAdd(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        String uuid = UUID.randomUUID().toString();
        String name = request.getParameter("name");
        String owner = request.getSession().getAttribute("username").toString();
        String description = request.getParameter("description");
        System.out.print(uuid + " " + name + " " + owner + " " + description);

        res = service.ModelAdd(uuid, name, owner, description);
        return res;
    }

    @RequestMapping(value = {"/modelUpdate.do"}, method = RequestMethod.POST)
    public NoteRequest ModelUpdate(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        NoteRequest res = service.ModelByUuid(uuid);
        if(res.getStatus() != 0) {
            return res;
        }
        Model model = (Model) res.getData();
        int lastVersion = model.getVersion();
        int version = lastVersion + 1;
        String modelString = request.getParameter("modelString");

        res = service.ModelUpdate(uuid, lastVersion, version, modelString);

        return res;
    }


    @RequestMapping(value = {"/getModelListAll.do"}, method = RequestMethod.POST)
    public NoteRequest ModelListAll(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int count=service.ModelListAllCount();
        int page_temp = page;
        int limit_temp = limit;
        if (count < page * limit) {
            limit = count - (page - 1) * limit;
        }
        page = (page_temp - 1) * limit_temp;
        NoteRequest res = service.ModelListAll(page,limit);
        res.setCount(count);
        return res;
    }


    @RequestMapping(value = {"/ModelListAllforName.do"}, method = RequestMethod.POST)
    public NoteRequest ModelListAllforName(HttpServletRequest request) {
        NoteRequest res = service.ModelListAllforName();
        return res;
    }

    @RequestMapping(value = {"/getModelListByOwner.do"}, method = RequestMethod.POST)
    public NoteRequest ModelListByOwner(HttpServletRequest request) {
        String owner = request.getParameter("owner");
        NoteRequest res = service.ModelListByOwner(owner);
        return res;
    }


    @RequestMapping(value = {"/getModelListMine.do"}, method = RequestMethod.POST)
    public NoteRequest ModelListMine(HttpServletRequest request) {
        System.out.println(request.getSession().getAttribute("username"));
        String owner = request.getSession().getAttribute("username").toString();
        NoteRequest res = service.ModelListByOwner(owner);
        return res;
    }

    @RequestMapping(value = {"/getModelByUuid.do"}, method = RequestMethod.POST)
    public NoteRequest ModelByUuid(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        NoteRequest res = service.ModelByUuid(uuid);
        return res;
    }

    @RequestMapping(value = {"/getModelByName.do"}, method = RequestMethod.POST)
    public NoteRequest ModelByName(HttpServletRequest request) {
        String name = request.getParameter("name");
        NoteRequest res = service.ModelByName(name);
        return res;
    }


    @RequestMapping(value = {"/modelRemoveByName.do"}, method = RequestMethod.POST)
    public NoteRequest ModelRemoveByName(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        String name = request.getParameter("name");
        res = service.ModelByName(name);
        if(res.getStatus() != 0) {
            return res;
        }
        Model model = (Model) res.getData();
        String modelUuid = model.getUuid();
        res = jobService.JobByModel(modelUuid);
        if(res.getStatus() != 0) {
            return res;
        }
        List<Job> jobs = (List<Job>) res.getData();
        for (Job job: jobs) {
//            if(!job.getState().equals("FINISH") && !job.getState().equals("FAIL")) {
//                res.setStatus(1);
//                res.setMsg("Job use this job not finished");
//                res.setData(null);
//                return res;
//            }
        }

        res = service.ModelRemoveByName(name);
        return res;
    }

}
